﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Office.InfoPath;

namespace InfoPathDebugger.FormOpenProvider
{
    public class FormDataOpenProvider : IFormOpenProvider
    {
        #region IFormOpenProvider Members

        public void Open(FormControl formControl, string filePath)
        {
            // Close the loaded InfoPath form.
            // Note: Close the form before clear InfoPath cache. Because some
            // files in the cache might be used in current InfoPath form, if the
            // form hasn't been closed, the files cannot be deleted.
            formControl.Close();

            // Clear the cache of InfoPath forms, prevent the version detect.
            InfoPathFormHelper.ClearInfoPathCache();

            // Popup a dialog to allow user to select an aproxiate template for the form data file.
            OpenFileDialog selectTemplateDlg = new OpenFileDialog();
            selectTemplateDlg.Title = "Select an InfoPath Template";
            selectTemplateDlg.Filter = "InfoPath files (*.xsn)|*.xsn";
            selectTemplateDlg.Multiselect = false;

            if (DialogResult.OK == selectTemplateDlg.ShowDialog())
            {
                InfoPathFormHelper.ModifyTemplateFile(filePath, selectTemplateDlg.FileName);
            }

            formControl.Open(filePath);
        }

        #endregion
    }
}
